/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/

package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.ImportUtil;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <dl>
 * <dd>描述: ~ OrderSettingController</dd>
 * <dd>创建时间：  10:01 2021/2/28</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/2/28      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {

    @DubboReference
    private OrderSettingService orderSettingService;

    /**
     * @Author: guodong
     * @Date: 10:34 2021/2/28
     * @ClassName: OrderSettingController
     * @Parms [multipartFile]
     * @ReturnType: Result
     * @Description: 上传excel批量设置可预约人数
    */
    @PostMapping("upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) {
        //获取源文件名称
        String filename = multipartFile.getOriginalFilename();
        if (StringUtils.isEmpty(filename)) {
            return new Result(false, "文件名称不能为空");
        }
        //文件读取
        List<List<Object>> lists;
        try {
            if (filename.endsWith(".xlsx")) { //07版
                lists = ImportUtil.read2007Excel(multipartFile.getInputStream());
            } else if (filename.endsWith(".xls")) { //03版
                lists = ImportUtil.read2003Excel(multipartFile.getInputStream());
            } else {
                return new Result(false, "非法文件名称");
            }
            //将 lists转换为 List<OrderSetting>
            List<OrderSetting> orderSettingList = parseToOrderSetting(lists);
            //rpc调用添加 List<OrderSetting>
            orderSettingService.add(orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.UPLOAD_FAIL);
        }
        return new Result(true, MessageConst.UPLOAD_SUCCESS);
    }
    /**
     * @Author: guodong
     * @Date: 10:12 2021/2/28
     * @ClassName: OrderSettingController
     * @Parms [lists]
     * @ReturnType: java.util.List<com.itheima.health.pojo.OrderSetting>
     * @Description: 将excel读取的数据转换为List<OrderSetting></>
    */
    private List<OrderSetting> parseToOrderSetting(List<List<Object>> lists) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<OrderSetting> orderSettingList = new ArrayList<>();
        for (int i = 1; i <lists.size(); i++) { //行 第一行数据忽略，因为是表头
            List<Object> cells = lists.get(i);
            //cells不建议遍历，防止越界数据(脏数据)
            String orderDate = (String) cells.get(0); //日期
            String num = (String) cells.get(1); //可预约人数
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setOrderDate(sdf.parse(orderDate));
            orderSetting.setNumber(Integer.valueOf(num));
            orderSettingList.add(orderSetting);
        }
        return orderSettingList;
    }

    /**
     * @Author: guodong
     * @Date: 11:06 2021/2/28
     * @ClassName: OrderSettingController
     * @Parms [startTime, endTime]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据日期查询当月预约信息
    */
    @GetMapping("findByDate")
    public Result findByDate(String startTime, String endTime){
        List<OrderSetting> orderSettings = orderSettingService.findByDate(startTime, endTime);
        List<Map<String, Object>> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate();
            calendar.setTime(orderDate);
            Map<String, Object> map = new HashMap<>();
            map.put("date", calendar.get(Calendar.DAY_OF_MONTH));
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            list.add(map);
        }
        return new Result(true, MessageConst.ACTION_SUCCESS,list);
    }

    /**
     * @Author: guodong
     * @Date: 11:46 2021/2/28
     * @ClassName: OrderSettingController
     * @Parms [orderSetting]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 设置可预约人数
    */
    @PostMapping("editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumByDate(orderSetting);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }
}
